package com.tuanbiao.ui;

import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Build;

import com.abiao.lib_utils.Utils;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.SplashController;
import com.tuanbiao.databinding.ActivitySlpashBinding;
import com.tuanbiao.dialog.BaseDialog;
import com.tuanbiao.dialog.CustomProgressDialog;
import com.tuanbiao.dialog.MessageDialog;
import com.tuanbiao.dialog.WaitDialog;
import com.tuanbiao.models.SplashModel;
import com.tuanbiao.presenter.SplashPresenter;
import com.tuanbiao.ui.account.LoginActivity;

import timber.log.Timber;

/**
 * 启动页
 */
public class SplashActivity extends BasePresenterActivity<SplashPresenter, ActivitySlpashBinding> implements SplashController.View {
    private CustomProgressDialog.Builder dialog;
    private MessageDialog.Builder killDialog;
    @Override
    protected SplashPresenter createPresenter() {
        return new SplashPresenter(new SplashModel(), this);
    }


    @Override
    protected ActivitySlpashBinding getBindingView() {
        return ActivitySlpashBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        setVolume();
    }

    private void setVolume() {
        AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        am.adjustStreamVolume (AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
    }
    @Override
    public void showMissingPermissionDialog(boolean shouldShowRequest) {
        Timber.d(TAG + "#showMissingPermissionDialog" + shouldShowRequest);
        new MessageDialog.Builder(getActivity())
                .setTitle(R.string.common_permission_alert)
                .setMessage(shouldShowRequest ? R.string.common_permission_fail_1 : R.string.common_permission_fail_2)
                .setListener(new MessageDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog) {
                        if (shouldShowRequest) mPresenter.checkUpdate();
                        else gotoSetting();
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        killMyself();
                    }
                })
                .show();
    }

    @Override
    public void startMain() {
        MainActivity.start(getActivity());
        killMyself();
    }

    @Override
    public void startLogin() {
        LoginActivity.start(getActivity());
    }

    @Override
    public void startConfig() {
        EncodingRulesActivity.start(getActivity());
    }

    @Override
    public void showProgress(String action, int progress) {
        if (null == dialog) {
            dialog = new CustomProgressDialog.Builder(getActivity());
            dialog.setProgress(0);
        }
        dialog.setMessage(action);
        dialog.setProgress(progress);
        if (!dialog.isShowing())
            dialog.show();
    }

    @Override
    public void showKillDialog(String msg) {
        if (null == killDialog) {
            killDialog = new MessageDialog.Builder(getActivity())
                    .setAlone(true)
                    .setCancelable(false)
                    .setListener(dialog -> killMyself())
                    .setMessage(msg);
        }
        if (!killDialog.isShowing()) killDialog.show();
    }

    @Override
    public void showUpgrade() {
        new MessageDialog.Builder(getActivity())
                .setTitle("固件升级")
                .setMessage("检测到有新版本固件，是否现在升级？")
                .setListener(new MessageDialog.OnListener() {
                    @Override
                    public void onConfirm(BaseDialog dialog) {
                        mPresenter.upgrade();
                    }

                    @Override
                    public void onCancel(BaseDialog dialog) {
                        mPresenter.checkStartApp();
                    }
                })
                .setCancelable(false)
                .show();
    }

    @Override
    public void showUpdate(String msg) {
        new WaitDialog.Builder(getActivity()).setMessage(msg)
                .show();
    }

    @Override
    public void showProgressDialog(int progress, String msg) {
        if (null == dialog) {
            dialog = new CustomProgressDialog.Builder(getActivity());
            dialog.setProgress(0);
        }
        dialog.setMessage(msg);
        dialog.setProgress(progress);
        if (!dialog.isShowing())
            dialog.show();
        if (progress >= 100) {
            dialog.dismiss();
            new MessageDialog.Builder(getActivity())
                    .setTitle("提示")
                    .setAlone(true)
                    .setListener(dialog -> mPresenter.checkStartApp())
                    .setMessage("升级成功")
                    .show();
        }
    }

    @Override
    public void updateFailed(String msg) {
        dialog.dismiss();
        new MessageDialog.Builder(getActivity())
                .setTitle("提示")
                .setMessage("升级失败" + msg)
                .show();
    }

    @Override
    public void updateFinish() {
        dialog.dismiss();
    }


    private void gotoSetting() {
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", Utils.getApp().getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", Utils.getApp().getPackageName());
        }
        startActivity(localIntent);
        killMyself();
    }

}
